﻿using BCCommon;
using BCData.Common.FAQ;
using BCDto.Common.FAQ;
using BCEntity.Common.EntityNameConst;
using BCEntity.Common.FAQ;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace BCService.Common.FAQ
{
    public class FaqService : IFaqService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IFaqData faqData;
        private readonly string key = string.Format("{0}", EntityNameConst.FaqEntity);

        public FaqService(IDatabaseContext databaseContext, 
            IRedisService redisService, 
            IFaqData faqData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.faqData = faqData;
        }

        public PageableList<FAQDto> QueryFAQ(long? faqId, string title, string keyWord, DateTime? start, DateTime? end, bool? ascending, bool fromSim, int pageIndex = 1, int pageSize = 10)
        {
            var list = this.faqData.QueryFAQ(faqId, title, keyWord, start, end, ascending, fromSim, out int dataCount, pageIndex, pageSize);
            return new PageableList<FAQDto>
            {
                Count = dataCount,
                PageIndex = pageIndex,
                PageSize = pageSize,
                Items = list.As<IEnumerable<FAQDto>>()
            };
        }

        public FAQDto NewAdd(string title, string question, string replyEmail)
        {
            FAQEntity entity = new FAQEntity();
            if (!string.IsNullOrWhiteSpace(replyEmail))
            {
                Regex regex = new Regex("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
                Match match = regex.Match(replyEmail);
                if (match.Success)
                {
                    entity = this.faqData.NewAdd(title, question, replyEmail);
                    return entity.As<FAQDto>();
                }
                throw new ArgumentException("邮箱格式不正确。");
            }
            entity = this.faqData.NewAdd(title, question, replyEmail);
            return entity.As<FAQDto>();
        }

        public FAQDto ModifyReply(long faqId, string answer, string keyWord, long adminId, string adminFullName)
        {
            var entity = this.faqData.ModifyReply(faqId, answer, keyWord, adminId, adminFullName);
            return entity.As<FAQDto>();
        }

        public FAQDto ModifyIsShow(long faqId, bool isShow)
        {
            var entity = this.faqData.ModifyIsShow(faqId, isShow);
            return entity.As<FAQDto>();
        }

        public bool ModifyIsDelete(long faqId)
        {
            return this.faqData.ModifyIsDelete(faqId);
        }

        public IEnumerable<FAQDto> QuerySpecify(int index, int amount)
        {
            var list = this.faqData.QuerySpecify(index, amount);
            return list.As<IEnumerable<FAQDto>>();
        }

        #region 检查常见问题ID是否存在
        public bool Exists(long faqId)
        {
            return this.faqData.Exists(faqId).GetAwaiter().GetResult();
        }
        #endregion


        #region 启用常见问题ID
        public bool Enabled(long faqId)
        {
            if (!this.Exists(faqId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.faqData.Enabled(faqId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, faqId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用常见问题ID
        public bool Disabled(long faqId)
        {
            if (!this.Exists(faqId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.faqData.Disabled(faqId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, faqId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}
